//
//  InlineFuncs.h
//  PointLegend
//
//  Created by 黄卫 on 17/2/24.
//  Copyright © 2017年 huangwei. All rights reserved.
//

#ifndef PointLegend_InlineFuncs_h
#define PointLegend_InlineFuncs_h

#import <CommonCrypto/CommonDigest.h>
#import "FD_Method.h"

static inline UIImage* imageWithColor(UIColor *clr)
{
    CGRect rect = CGRectMake(0, 0, 1, 1);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    CGContextSetFillColorWithColor(context, [clr CGColor]);
    CGContextFillRect(context, rect);
    
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return image;
}

static inline NSString * md5(NSString *str)
{
    const char *cStr = [str UTF8String];
    unsigned char digest[CC_MD5_DIGEST_LENGTH];
    CC_MD5( cStr, str.length, digest );
    NSMutableString *result = [[NSMutableString alloc] initWithString:@""];
    for(int i = 0; i < CC_MD5_DIGEST_LENGTH; i++)
        [result appendFormat:@"%02x", digest[i]];
    return result;
}

static inline UIImage * imageByApplyingAlpha(CGFloat alpha,UIImage *image){
    UIGraphicsBeginImageContextWithOptions(image.size, NO, 0.0f);
    
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    CGRect area = CGRectMake(0, 0, image.size.width, image.size.height);
    
    CGContextScaleCTM(ctx, 1, -1);
    CGContextTranslateCTM(ctx, 0, -area.size.height);
    
    CGContextSetBlendMode(ctx, kCGBlendModeMultiply);
    
    CGContextSetAlpha(ctx, alpha);
    
    CGContextDrawImage(ctx, area, image.CGImage);
    
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    
    UIGraphicsEndImageContext();
    
    return newImage;
}

static inline NSDate* localDateWithDate(NSDate *date)
{
    NSTimeZone *timeZone = [NSTimeZone systemTimeZone];
    NSInteger interval = [timeZone secondsFromGMTForDate:date];
    NSDate *localDate = [date dateByAddingTimeInterval:interval];
    return localDate;
}

static inline BOOL isOfClasses(id obj,NSArray *classes)
{
    BOOL ret = NO;
    int i = 0;
    while (i < classes.count) {
        if ([obj isKindOfClass:NSClassFromString(classes[i])]) {
            ret = YES;
            break;
        }
        i++;
    }
    return ret;
}

static inline NSString *generateOrderId(NSString *shopId)
{
    if (shopId.length==0) {
        return @"";
    }
    NSString *retStr=@"";
    if (shopId.length<10) {
        int len = shopId.length;
        for (int i = 0; i < 10-len; i++) {
            retStr = [retStr stringByAppendingString:@"0"];
        }
        retStr = [retStr stringByAppendingString:shopId];
    }
    
    NSDate *date = [NSDate date];
//    date = localDateWithDate(date);
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSString *currentDateStr = [dateFormatter stringFromDate:date];
    currentDateStr = ^{
        NSString *ret = @"";
        NSArray *arr = [currentDateStr componentsSeparatedByString:@" "];
        if (arr.count==2) {
            NSArray *dayArr = [arr.firstObject componentsSeparatedByString:@"-"];
            for (NSString *str in dayArr) {
                ret = [ret stringByAppendingString:str];
            }
            NSArray *hourArr = [arr[1] componentsSeparatedByString:@":"];
            for (NSString *str in hourArr) {
                ret = [ret stringByAppendingString:str];
            }
        }
        return ret;
    }();
    
    retStr = [retStr stringByAppendingString:currentDateStr];
    
    int x = arc4random() % (99999-10000+1)+10000;
    
    retStr = [retStr stringByAppendingFormat:@"%d",x];
    return retStr;
}

static inline NSString * dateString(NSDate *date)
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSString *currentDateStr = [dateFormatter stringFromDate:date];
    return currentDateStr;
}


static inline NSString * dateStringWithoutHMS(NSDate *date)
{
//    date = localDateWithDate(date);
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSString *currentDateStr = [dateFormatter stringFromDate:date];
    return [currentDateStr componentsSeparatedByString:@" "].firstObject;
}

static inline NSDate *dateFromString(NSString *str)
{
    NSDateFormatter *dateFormatter=[[NSDateFormatter alloc]init];
    /** 直接指定时区--东八区*/
    NSTimeZone* GTMzone = [NSTimeZone timeZoneForSecondsFromGMT:8 * 3600];
    [dateFormatter setTimeZone:GTMzone];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *date = [dateFormatter dateFromString:str];
    return date;
}

static inline NSString *getYearAndMonth(NSString *str,BOOL isStart){
    NSArray *arr = [str componentsSeparatedByString:@"-"];
    NSMutableString *dateStr = [NSMutableString stringWithString:@""];
    if (arr.count==3) {
        [dateStr appendString:arr[0]];
        [dateStr appendString:@"-"];
        [dateStr appendString:arr[1]];
    }
    NSInteger days = [FD_Method getCurenDateMothDays:dateFromString([NSString stringWithFormat:@"%@-01 08:00:00",dateStr])];
    [dateStr appendString:!isStart?[NSString stringWithFormat:@"-%ld",(long)days]:@"-01"];
    return (NSString *)dateStr;
}

static inline NSString *chatDate2String(NSString *oldDate)
{
    if (!isValidString(oldDate)) {
        return nil;
    }
    NSArray *arr = [oldDate componentsSeparatedByString:@" "];
    if (arr.count==2) {
        NSDate *date = dateFromString(oldDate);
    }
    return nil;
}

//验证身份证
//必须满足以下规则
//1. 长度必须是18位，前17位必须是数字，第十八位可以是数字或X
//2. 前两位必须是以下情形中的一种：11,12,13,14,15,21,22,23,31,32,33,34,35,36,37,41,42,43,44,45,46,50,51,52,53,54,61,62,63,64,65,71,81,82,91
//3. 第7到第14位出生年月日。第7到第10位为出生年份；11到12位表示月份，范围为01-12；13到14位为合法的日期
//4. 第17位表示性别，双数表示女，单数表示男
//5. 第18位为前17位的校验位
//算法如下：
//（1）校验和 = (n1 + n11) * 7 + (n2 + n12) * 9 + (n3 + n13) * 10 + (n4 + n14) * 5 + (n5 + n15) * 8 + (n6 + n16) * 4 + (n7 + n17) * 2 + n8 + n9 * 6 + n10 * 3，其中n数值，表示第几位的数字
//（2）余数 ＝ 校验和 % 11
//（3）如果余数为0，校验位应为1，余数为1到10校验位应为字符串“0X98765432”(不包括分号)的第余数位的值（比如余数等于3，校验位应为9）
//6. 出生年份的前两位必须是19或20
static inline bool verifyIDCardNumber(NSString *value)
{
    value = [value stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    if ([value length] != 18) {
            return NO;
    }
    NSString *mmdd = @"(((0[13578]|1[02])(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)(0[1-9]|[12][0-9]|30))|(02(0[1-9]|[1][0-9]|2[0-8])))";
    NSString *leapMmdd = @"0229";
    NSString *year = @"(19|20)[0-9]{2}";
    NSString *leapYear = @"(19|20)(0[48]|[2468][048]|[13579][26])";
    NSString *yearMmdd = [NSString stringWithFormat:@"%@%@", year, mmdd];
    NSString *leapyearMmdd = [NSString stringWithFormat:@"%@%@", leapYear, leapMmdd];
    NSString *yyyyMmdd = [NSString stringWithFormat:@"((%@)|(%@)|(%@))", yearMmdd, leapyearMmdd, @"20000229"];
    NSString *area = @"(1[1-5]|2[1-3]|3[1-7]|4[1-6]|5[0-4]|6[1-5]|82|[7-9]1)[0-9]{4}";
    NSString *regex = [NSString stringWithFormat:@"%@%@%@", area, yyyyMmdd  , @"[0-9]{3}[0-9Xx]"];
    NSPredicate *regexTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    if (![regexTest evaluateWithObject:value]) {
               return NO;
    }
    int summary = ([value substringWithRange:NSMakeRange(0,1)].intValue + [value substringWithRange:NSMakeRange(10,1)].intValue) *7
            + ([value substringWithRange:NSMakeRange(1,1)].intValue + [value substringWithRange:NSMakeRange(11,1)].intValue) *9
            + ([value substringWithRange:NSMakeRange(2,1)].intValue + [value substringWithRange:NSMakeRange(12,1)].intValue) *10
            + ([value substringWithRange:NSMakeRange(3,1)].intValue + [value substringWithRange:NSMakeRange(13,1)].intValue) *5
            + ([value substringWithRange:NSMakeRange(4,1)].intValue + [value substringWithRange:NSMakeRange(14,1)].intValue) *8
            + ([value substringWithRange:NSMakeRange(5,1)].intValue + [value substringWithRange:NSMakeRange(15,1)].intValue) *4
            + ([value substringWithRange:NSMakeRange(6,1)].intValue + [value substringWithRange:NSMakeRange(16,1)].intValue) *2
            + [value substringWithRange:NSMakeRange(7,1)].intValue *1 + [value substringWithRange:NSMakeRange(8,1)].intValue *6
            + [value substringWithRange:NSMakeRange(9,1)].intValue *3;
    NSInteger remainder = summary % 11;
    NSString *checkBit = @"";
    NSString *checkString = @"10X98765432";
    checkBit = [checkString substringWithRange:NSMakeRange(remainder,1)];// 判断校验位
    BOOL ret = [checkBit isEqualToString:[[value substringWithRange:NSMakeRange(17,1)] uppercaseString]];
    return ret;
}

/*   ViewController初始化
 
 @interface GJDepositVC ()
 
 @end
 
 @implementation GJDepositVC
 
 #pragma mark LifeCycle
 
 - (void)dealloc
 {
 NSLog(@"%s",__func__);
 }
 
 - (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
 {
 if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
 
 }
 return self;
 }
 
 - (void)viewDidLoad {
 [super viewDidLoad];
 // Do any additional setup after loading the view.
 }
 
 #pragma mark Network
 
 #pragma mark Delegate
 
 #pragma mark Others
 
 @end
 */

#endif
